package servlets;

import java.io.IOException;
import java.sql.Date;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sessions.UserManagerRemote;

/**
 * Servlet implementation class Edit
 */
public class Edit extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Edit() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		
		String username = (String) session.getAttribute("username");
		String password = request.getParameter("password");
		String pswCtrl = request.getParameter("pswControl");
		String email = request.getParameter("email");
		String name = request.getParameter("name");
		String surname = request.getParameter("surname");
		String b = request.getParameter("birth");
		String address = request.getParameter("address");
		
		Pattern pattern;
		Matcher matcher;
		final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
				+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		pattern = Pattern.compile(EMAIL_PATTERN);
		matcher = pattern.matcher(email);

		if (password == null || password.length() <= 0 || email == null
				|| email.length() <= 0 || name == null || name.length() <= 0
				|| surname == null || surname.length() <= 0) {
			String error = "<font face='tahoma' size='2'>I campi segnati con l'asterisco (*) sono obbligatori.</font><br><font face='tahoma' size='2'>Torna al <a href='./MyProfile' style='color: blue; text-decoration: underline'>profilo</a>.</font>";
			session.setAttribute("error", error);
			response.sendRedirect("error.jsp");
			return;
		}

		if (!password.equals(pswCtrl)) {
			String error = "<font face='tahoma' size='2'>La password di controllo inserita non è corretta.</font><br><font face='tahoma' size='2'>Torna al <a href='./MyProfile' style='color: blue; text-decoration: underline'>profilo</a>.</font>";
			session.setAttribute("error", error);
			response.sendRedirect("error.jsp");
			return;
		}

		if (!matcher.matches()) {
			String error = "<font face='tahoma' size='2'>L'indirizzo email inserito non è corretto.</font><br><font face='tahoma' size='2'>Torna al <a href='./MyProfile' style='color: blue; text-decoration: underline'>profilo</a>.</font>";
			session.setAttribute("error", error);
			response.sendRedirect("error.jsp");
			return;
		}
		
		if (address == "") {
			address = null;
		}
		
		Date birth = null;
		try {
			if (b != "") {
				birth = Date.valueOf(b);
			}
		} catch (IllegalArgumentException ex) {
			String error = "<font face='tahoma' size='2'>La data non è stata inserita nel giusto formato.</font><br><font face='tahoma' size='2'>Torna al <a href='./MyProfile' style='color: blue; text-decoration: underline'>profilo</a>.</font>";
			session.setAttribute("error", error);
			response.sendRedirect("error.jsp");
			return;
		}

		try {
			Hashtable<String, String> env = new Hashtable<String, String>();
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL, "localhost:1099");
			InitialContext jndiContext = new InitialContext(env);
			Object ref = jndiContext.lookup("UserManager/remote");
			UserManagerRemote r = (UserManagerRemote) ref;
			r.edit(username, password, email, name, surname, birth, address);
			response.sendRedirect("./MyProfile");
			return;
		} catch (NamingException e) {
			e.printStackTrace();
		}

	}

}
